$(document).ready(function(){
	var startpage=0;
	getData();
	$.post("manager/getlogin",
	        function(data){
	        
	        console.log(data); 
	        var loginname=data.name;
	       console.log(loginname); 
	       $("#helloname").text(loginname);
	       
	        }, "json");
	$("#selectbtn2").click(function(){
		$("#selectname").val("");
		getData();
	});
	 function getData(){
		 $.getJSON("warning/showWarninglist",{start:startpage},function(json){
			 console.log(json);
			 var contentdata=json.content;
			 $("#tbodyprobtn").empty();
			 for(var i=0;i<contentdata.length;i++){
				 var status="";
				 var min="";
				 var max="";
				 if(contentdata[i][3]==null){
					 status="";
					 min="";
					 max="";
				 }else{
					 status=contentdata[i][3];
					 min=contentdata[i][5];
					 max=contentdata[i][4];
				 }
				 $("#tbodyprobtn").append(
						 "<tr id='tridval"+contentdata[i][0]+"'>"
						 +"<td>"+contentdata[i][0]
						 +"</td>"
						 +"<td>"+contentdata[i][1]
						 +"</td>"
						 +"<td>"+contentdata[i][2]
						 +"</td>"
						 +"<td>"+status
						 +"</td>"
						 +"<td>"+min
						 +"</td>"
						 +"<td>"+max
						 +"</td>"
						 +"<td>"
						 +"<button type='button' class='btn btn-outline-info btn-sm' id='btn1"+contentdata[i][0]+"' name='btn001' >修改</button>"
						 +"</td></tr>"
				 ) 
				 $("#tbodyprobtn").append(
				"<tr style='display:none' id='tridval2"+contentdata[i][0]+"'><form>"  
				+"<td id='id2"+contentdata[i][0]+"'>"+contentdata[i][0] 
				 +"</td>"
				 +"<td>"+contentdata[i][1]
				 +"</td>"
				 +"<td>"+contentdata[i][2]
				 +"</td>"
				 +"<td>"+status
				 +"</td>"
				+"<td><input type='text' style='width:85px' id='minnum"+contentdata[i][0]+"' name='minnum' value='"+min
				+"'/></td>"
				+"<td><input type='text' style='width:85px' id='maxnum"+contentdata[i][0]+"' value='"+max  
				+"'/></td>"
				+"<td> <button type='button' name='btn002' class='btn btn-primary btn-sm' id='btn2"+contentdata[i][0]+"'>保存</button>"
				+"</td><form></tr>"  
				)
			 }
			
			 //--修改开始--//
			 
				$("button[name='btn001']").click(function(){
					var id=this.id;
					var numb=id.slice(4);
					$("#tridval"+numb).hide();
					$("#tridval2"+numb).show();
					$("#productid").val(numb);
				});
				//保存修改
				$("button[name='btn002']").click(function(){
					var id=this.id;
					var numb=id.slice(4);
					var mixval=$("#minnum"+numb).val();
					var maxval=$("#maxnum"+numb).val();
					var proval=$("#productid").val();
					console.log(proval);
					$.getJSON("warning/savewarning",{productid:proval,minnum:mixval,maxnum:maxval},function(json){
						console.log(json);
						getData();
						
					})
				});
			 //--修改结束--//
			
			//---分页开始---//
			  var totalPagesnumber=json.totalPages;
			  $(".pagination").empty();
			  $(".pagination").append('<li class=""><a class="page-link" href="#" id="firstpage">首页</a></li>' );
			  $(".pagination").append('<li class=""><a class="page-link" href="#" id="previouspage">前一页</a></li>' );
			  for(var j=0;j<totalPagesnumber;j++){
				  $(".pagination").append(' <li class="page-item" id="pageno'+j+'"><a class="page-link" href="#">'+(j+1)+'</a></li>');
			  }
			  $(".pagination").append('<li class=""><a class="page-link" href="#" id="nextpage">下一页</a></li>' );
			  $(".pagination").append('<li class=""><a class="page-link" href="#" id="lastpage">尾页</a></li>' );
			  
			//把当前页，颜色变为蓝色
				$(".page-item").removeClass("active");
				$("#pageno"+startpage).addClass("active");
				//分页跳转
				$(".page-item").click(function(){
					var idval=this.id;
					startpage=idval.substr(6);
					getData();
				}) 
					
				//上一页
				$("#previouspage").click(function(){
					var curr=new Number(startpage);
					if(curr>0){
						startpage=curr-1;
					}else{
						startpage=0;
					}
					
					getData();
				})
						
				//下一页
				$("#nextpage").click(function(){
					var curr=new Number(startpage) ;
					if(curr<totalPagesnumber-1){
						startpage=curr+1;
					}else{
						startpage=totalPagesnumber-1;
					}
					
					getData();
				})
				
				 //首页
				 $("#firstpage").click(function(){
					 startpage=0;
					 getData();
				 });
				
				//尾页
				$("#lastpage").click(function(){
					startpage=totalPagesnumber-1;
					getData();
				})
			 //--分页结束--//
		 });
	 }
	 //--查询开始--//
	$("#selectbtn").click(function(){
		var startpage2=0;
		getname();
	 function getname(){
		 $.getJSON("warning/showWarninglistByProName",{start:startpage2,selectname:$("#selectname").val()},function(json){
			 console.log(json);
			 var contentdata=json.content;
			 $("#tbodyprobtn").empty();
			 for(var i=0;i<contentdata.length;i++){
				 var status="";
				 var min="";
				 var max="";
				 if(contentdata[i][3]==null){
					 status="";
					 min="";
					 max="";
				 }else{
					 status=contentdata[i][3];
					 min=contentdata[i][5];
					 max=contentdata[i][4];
				 }
				 $("#tbodyprobtn").append(
						 "<tr id='tridval"+contentdata[i][0]+"'>"
						 +"<td>"+contentdata[i][0]
						 +"</td>"
						 +"<td>"+contentdata[i][1]
						 +"</td>"
						 +"<td>"+contentdata[i][2]
						 +"</td>"
						 +"<td>"+status
						 +"</td>"
						 +"<td>"+min
						 +"</td>"
						 +"<td>"+max
						 +"</td>"
						 +"<td>"
						 +"<button type='button' class='btn btn-outline-info btn-sm' id='btn1"+contentdata[i][0]+"' name='btn001' >修改</button>"
						 +"</td></tr>"
				 ) 
				 $("#tbodyprobtn").append(
				"<tr style='display:none' id='tridval2"+contentdata[i][0]+"'><form>"  
				+"<td id='id2"+contentdata[i][0]+"'>"+contentdata[i][0] 
				 +"</td>"
				 +"<td>"+contentdata[i][1]
				 +"</td>"
				 +"<td>"+contentdata[i][2]
				 +"</td>"
				 +"<td>"+status
				 +"</td>"
				+"<td><input type='text' style='width:85px' id='minnum"+contentdata[i][0]+"' name='minnum' value='"+min
				+"'/></td>"
				+"<td><input type='text' style='width:85px' id='maxnum"+contentdata[i][0]+"' value='"+max  
				+"'/></td>"
				+"<td> <button type='button' name='btn002' class='btn btn-primary btn-sm' id='btn2"+contentdata[i][0]+"'>保存</button>"
				+"</td><form></tr>"  
				)
			 }
			
			 //--修改开始--//
			 
				$("button[name='btn001']").unbind('click').click(function(){
					var id=this.id;
					var numb=id.slice(4);
					$("#tridval"+numb).hide();
					$("#tridval2"+numb).show();
					$("#productid").val(numb);
				});
				//保存修改
				$("button[name='btn002']").unbind('click').click(function(){
					var id=this.id;
					var numb=id.slice(4);
					var mixval=$("#minnum"+numb).val();
					var maxval=$("#maxnum"+numb).val();
					var proval=$("#productid").val();
					console.log(proval);
					$.getJSON("warning/savewarning",{productid:proval,minnum:mixval,maxnum:maxval},function(json){
						console.log(json);
						getData();
						
					})
				});
			 //--修改结束--//
			
			//---分页开始---//
			  var totalPagesnumber=json.totalPages;
			  $(".pagination").empty();
			  $(".pagination").append('<li class=""><a class="page-link" href="#" id="firstpage">首页</a></li>' );
			  $(".pagination").append('<li class=""><a class="page-link" href="#" id="previouspage">前一页</a></li>' );
			  for(var j=0;j<totalPagesnumber;j++){
				  $(".pagination").append(' <li class="page-item" id="pageno'+j+'"><a class="page-link" href="#">'+(j+1)+'</a></li>');
			  }
			  $(".pagination").append('<li class=""><a class="page-link" href="#" id="nextpage">下一页</a></li>' );
			  $(".pagination").append('<li class=""><a class="page-link" href="#" id="lastpage">尾页</a></li>' );
			  
			//把当前页，颜色变为蓝色
				$(".page-item").removeClass("active");
				$("#pageno"+startpage2).addClass("active");
				//分页跳转
				$(".page-item").click(function(){
					var idval=this.id;
					startpage2=idval.substr(6);
					getname();
				}) 
					
				//上一页
				$("#previouspage").click(function(){
					var curr=new Number(startpage2);
					if(curr>0){
						startpage2=curr-1;
					}else{
						startpage2=0;
					}
					
					getname();
				})
						
				//下一页
				$("#nextpage").click(function(){
					var curr=new Number(startpage2) ;
					if(curr<totalPagesnumber-1){
						startpage2=curr+1;
					}else{
						startpage2=totalPagesnumber-1;
					}
					
					getname();
				})
				
				 //首页
				 $("#firstpage").click(function(){
					 startpage2=0;
					 getname();
				 });
				
				//尾页
				$("#lastpage").click(function(){
					startpage2=totalPagesnumber-1;
					getname();
				})
			 //--分页结束--//
		 });
	 }
	});
});